package com.lanou.zy.projecta.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.FindNextStepRecyclerViewAdapter;
import com.lanou.zy.projecta.entity.FindNextStepAll;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.util.PullToRefreshView;
import com.lanou.zy.projecta.util.RefreshableView;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 * 发现下一站的活动界面
 * 点击事件是调用系统的back键
 */
public class FindNextStepActivity extends Activity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private FindNextStepRecyclerViewAdapter findNextStepRecyclerViewAdapter;
    private RequestQueue queue;
    private ImageView backToRecommend_Iv;
    private List<FindNextStepAll.DataEntity> fd;
    //    private RefreshableView refreshableView;
    private PullToRefreshView mPullToRefreshView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findnextstep);
        initView();
        initData();
    }

    private void initView() {
//        refreshableView = (RefreshableView) findViewById(R.id.refreshable_view);
        backToRecommend_Iv = (ImageView) findViewById(R.id.backToRecommend_Iv);
        backToRecommend_Iv.setOnClickListener(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = (RecyclerView) findViewById(R.id.findNextStepRecyclerView);
        recyclerView.setLayoutManager(gridLayoutManager);
        findNextStepRecyclerViewAdapter = new FindNextStepRecyclerViewAdapter(this);
        recyclerView.setAdapter(findNextStepRecyclerViewAdapter);

        findNextStepRecyclerViewAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(FindNextStepActivity.this, WebViewActivity.class);

                intent.putExtra("url", fd.get(position).getUrl());
                intent.putExtra("name", fd.get(position).getTitle());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });


        mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 2000);
            }
        });


//        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
//            @Override
//            public void onRefresh() {
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                refreshableView.finishRefreshing();
//            }
//        }, 0);
    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        String url = "http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.883721&lon=121.544776&page=1&count=10";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FindNextStepAll fnsa = gson.fromJson(response, FindNextStepAll.class);
                fd = fnsa.getData();
                findNextStepRecyclerViewAdapter.addData(fd);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backToRecommend_Iv:
                onBackPressed();
                break;
        }
    }
}
